import request from '@/utils/request'

export const useFetch = () => {
  const loading = ref(false)
  return {
    fetch: {
      get: async (url: string, params?: any) => {
        loading.value = true
        return request.get(url, { params }).finally(() => {
          loading.value = false
        })
      },
      post: async (url: string, data?: any) => {
        loading.value = true
        return request.post(url, data).finally(() => {
          loading.value = false
        })
      },
      put: async (url: string, data?: any) => {
        loading.value = true
        request.put(url, data).finally(() => {
          loading.value = false
        })
      },
      delete: async (url: string, params?: any) => {
        loading.value = true
        request.delete(url, { params }).finally(() => {
          loading.value = false
        })
      },
    },
    loading,
  }
}
